#include "QLogTool.h"
#include <QDateTime>
#include <QDebug>
#include <map>

using namespace std;
namespace  {
    const size_t CLI_LEN_MAX = 2048;
}

void LOGBox(const string& title, const char *str, ...)
{
    static char LogBoxStr[CLI_LEN_MAX];
    va_list args;
    va_start(args, str);
    vsprintf(LogBoxStr, str, args);
    va_end(args);

    string info(LogBoxStr);
    LOG(title + ":" + info);
    QLogTool::Instance().LogBox(QString::fromStdString(title), QString::fromStdString(info));
}

void LOGBox(const std::string& title, QString str)
{
    LOGBox(title, str.toLatin1().data());
}

void LOG(const char *str, ...)
{
    static char CLI_STR[CLI_LEN_MAX];
    va_list args;
    va_start(args, str);
    vsprintf(CLI_STR, str, args);
    va_end(args);

    LOG(QString(CLI_STR));
}

void LOG(string str)
{
    LOG(QString::fromStdString(str));
}

void LOG(QString qLog)
{
    qLog = QDateTime::currentDateTime().toString("MM.dd hh:mm:ss.zzz ") + qLog;
    qDebug() << qLog;
    // 通过QLogTool发射信号
    QLogTool::Instance().LOGStaBar(qLog);
}

string ReUnitSize(int num, DATA_TYPE src, DATA_TYPE dst)
{
    double sizeAsByte = 0.0;
    double sizeInput = static_cast<double>(num);
    switch (src) {
    case b: sizeAsByte = sizeInput / 8.0;
        break;
    case B: sizeAsByte = sizeInput;
        break;
    case KB:sizeAsByte = sizeInput * 1024;
        break;
    case MB:sizeAsByte = sizeInput * 1024 * 1024;
        break;
    case GB:sizeAsByte = sizeInput * 1024 * 1024 * 1024;
        break;
    }
    switch (dst) {
    case b:
        return to_string(sizeAsByte*8) + " b";
    case B:
        return to_string(sizeAsByte) + " B";
    case KB:
        return to_string(sizeAsByte/1024) + " KB";
    case MB:
        return to_string(sizeAsByte/1024/1024) + " MB";
    case GB:
        return to_string(sizeAsByte/1024/1024/1024) + " GB";
    }
    return "";
}
